﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Xin478.CSWork.Common;
using Xin478.MySocket;

namespace Xin478.CSWork.Client
{
    public partial class UserListForm : MyForm
    {
        IClient client;
        EmployeeInfo choosedEmployeeInfo;
        public EmployeeInfo ChoosedEmployeeInfo { get { return choosedEmployeeInfo; } set { choosedEmployeeInfo = value; } }

        public UserListForm(IClient client)
        {
            InitializeComponent();
            this.client = client;
            this.Load += new EventHandler(UserListForm_Load);
            this.Closing += new System.ComponentModel.CancelEventHandler(UserListForm_Closing);
        }

        void UserListForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            client.CommandReceived-= new CommandReceiveHandler(CommandReceive);
            e.Cancel = false;
            this.DialogResult = DialogResult.OK;
        }

        void UserListForm_Load(object sender, EventArgs e)
        {
            client.CommandReceived += new CommandReceiveHandler(CommandReceive); 
            RefeshData();
        }

        void CommandReceive(DatagramBuilder builder, ref bool cancel)
        {
            MethodInfo mi = this.GetType().GetMethod(builder.Name, BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.Instance);
            if (mi != null)
            {
                mi.Invoke(this, builder.Params);
                cancel = true;
            }
        }

        public void RefeshData()
        {
            this.client.Send("Group.列表");
        }

        void BindGroupList(IList<GroupInfo> groupInfos)
        {
            BindGroupList(groupInfos, true);
        }
        void BindGroupList(IList<GroupInfo> groupInfos, bool requestUser)
        {
            this.myTreeView2.Nodes.Clear();

            if (groupInfos == null) return;
            foreach (GroupInfo groupInfo in groupInfos)
            {
                BindGroup(groupInfo, requestUser);
            }
        }
        void BindGroup(GroupInfo groupInfo, bool requestUser)
        {
            TreeNode node = new TreeNode();
            node.Name = groupInfo.Id.ToString();
            node.Text = groupInfo.Name;
            this.myTreeView2.Nodes.Add(node);

            if (requestUser)
                this.client.Send("User.用户列表", groupInfo.Id);
        }
        void BindEmployeeList(int groupId, IList<EmployeeInfo> employeeInfos)
        {
            TreeNode 用户列表;
            if (this.myTreeView2.Nodes.ContainsKey(groupId.ToString()))
                用户列表 = this.myTreeView2.Nodes[groupId.ToString()];
            else
                return;

            用户列表.Nodes.Clear();
            foreach (EmployeeInfo employeeInfo in employeeInfos)
            {
                TreeNode node = new TreeNode();
                node.Text =employeeInfo.UserName+" ["+ employeeInfo.Name+"]";
                node.Tag = employeeInfo;
                if (employeeInfo.IsOnline) node.ForeColor = Color.Blue;
                用户列表.Nodes.Add(node);
            }
        }

        void 部门列表(NetEventArgs e, IList<GroupInfo> groupInfos)
        {
            WinForm.DoThreadSafe(this.myTreeView2, delegate()
            {
                BindGroupList(groupInfos);
            });
        }
        void 部门用户列表(NetEventArgs e, int groupId, IList<EmployeeInfo> employeeInfos)
        {
            WinForm.DoThreadSafe(this.myTreeView2, delegate() { BindEmployeeList(groupId, employeeInfos); });
        }

        void pictureBox1_Click(object sender, EventArgs e)
        {
            this.myTreeView2.Nodes.Clear();
            RefeshData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            choosedEmployeeInfo = null;
            foreach (TreeNode pn in this.myTreeView2.Nodes)
            {
                foreach (TreeNode cn in pn.Nodes)
                {
                    if (cn.IsSelected)
                    {
                        choosedEmployeeInfo = cn.Tag as EmployeeInfo;
                        break;
                    }
                }
                if (choosedEmployeeInfo!=null) break;
            }
            this.Close();
        }
    }
}
